package org.example.rabbitmq.config;

import org.example.rabbitmq.constant.Constant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    //发布订阅模式
    @Bean("fanoutQueue1")
    public Queue fanoutQueue1() {
        return QueueBuilder.durable(Constant.FANOUT_QUEUE1).build();
    }
    @Bean("fanoutQueue2")
    public Queue fanoutQueue2() {
        return QueueBuilder.durable(Constant.FANOUT_QUEUE2).build();
    }
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange() {
        return ExchangeBuilder.fanoutExchange(Constant.FANOUT_EXCHANGE).durable(true).build();
    }
    //这里的第一个参数是Exchange的时候 ，在绑定的时候后面需要加noargs();
    //idea专业版好像应该不能弄了
    @Bean("fanoutBinding1")
    public Binding fanoutBinding1(@Qualifier("fanoutExchange")FanoutExchange fanoutExchange , @Qualifier("fanoutQueue1") Queue fanoutQueue1) {
        return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
    }
    @Bean("fanoutBinding2")
    public Binding fanoutBinding2(@Qualifier("fanoutExchange") FanoutExchange fanoutExchange, @Qualifier("fanoutQueue2") Queue fanoutQueue2) {
        return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
    }
    //路由模式
    @Bean("directQueue1")
    public Queue directQueue1() {
        return QueueBuilder.durable(Constant.DIRECT_QUEUE1).build();
    }
    @Bean("directQueue2")
    public Queue directQueue2() {
        return QueueBuilder.durable(Constant.DIRECT_QUEUE2).build();
    }
    @Bean("directExchange")
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange(Constant.DIRECT_EXCHANGE).durable(true).build();
    }
    @Bean("directBinding1")
    public Binding directBinding1(@Qualifier("directExchange") DirectExchange directExchange, @Qualifier("directQueue1") Queue directQueue1) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with("orange");
    }
    @Bean("directBinding2")
    public Binding directBinding2(@Qualifier("directExchange") DirectExchange directExchange, @Qualifier("directQueue2") Queue directQueue2) {
        return BindingBuilder.bind(directQueue2).to(directExchange).with("black");
    }
    @Bean("directBinding3")
    public Binding directBinding3(@Qualifier("directExchange") DirectExchange directExchange, @Qualifier("directQueue2") Queue directQueue2) {
        return BindingBuilder.bind(directQueue2).to(directExchange).with("orange");
    }
    //通配符模式
    @Bean("topicQueue1")
    public Queue topicQueue1() {
        return QueueBuilder.durable(Constant.TOPIC_QUEUE1).build();
    }
    @Bean("topicQueue2")
    public Queue topicQueue2() {
        return QueueBuilder.durable(Constant.TOPIC_QUEUE2).build();
    }
    @Bean("topicExchange")
    public TopicExchange topicExchange() {
        return ExchangeBuilder.topicExchange(Constant.TOPIC_EXCHANGE).durable(true).build();
    }
    @Bean("bindingTopic1")
    public Binding bindingTopic1(@Qualifier("topicExchange")TopicExchange topicExchange, @Qualifier("topicQueue1") Queue topicQueue1) {
        return BindingBuilder.bind(topicQueue1).to(topicExchange).with("*.orange.*");
    }
    @Bean("bindingTopic2")
    public Binding bindingTopic2(@Qualifier("topicExchange")TopicExchange topicExchange, @Qualifier("topicQueue2") Queue topicQueue2) {
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with("*.*.rabbit");
    }
    //*是单词
    //#是任何长度的东西
    @Bean("bindingTopic3")
    public Binding bindingTopic3(@Qualifier("topicExchange")TopicExchange topicExchange, @Qualifier("topicQueue2") Queue topicQueue2) {
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with("lazy.#");
    }

}
